package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.ICommunityRankVOService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.CommunityRankVO;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 将需要的数据初始化到redis中
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Reference
    private ICommunityRankVOService communityRankVOService;

    @Reference
    private IUserInfoService userInfoService;

    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        /**
         * 目的: 将我们的攻略数据初始化到redis中
         * 步骤: 1.获取到我们所有的攻略
         *      2.遍历写入redis中  注意: 有些数据是需要我们手动加入
         *      注意!!!: 如果redis中已存在这个数据,那么我们就不需要在写入覆盖这个数据,以免造成数据丢失!!!
         */
        //初始化国内外热门攻略
        /*System.out.println("---------------begin---------------");
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            int num = detail.getFavornum() + detail.getThumbsupnum();
            strategyStatisVOService.addAbroadorUnabroad(detail.getId(), num);
        }
        System.out.println("---------------end---------------");*/


        // 初始化社区排行的vo对象
        System.out.println("------------------初始化社区排行vo对象-------------------");

        List<CommunityRankVO> rankVOList = communityRankVOService.list();
        for (CommunityRankVO rankVO : rankVOList) {
            //判断是否存在vo对象
            if (communityRankVOService.hasVoKey(rankVO.getId())) {
                continue;
            }
            //存入vo对象
            communityRankVOService.setCommunityRankVO(rankVO);
        }

        System.out.println("--------------------end-----------------------");



        // 初始化9个排行相关的zset  ===================================
        // 这里金牌数据只有初始化,没有做加分

        /*System.out.println("------------------------今日金牌begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            // 这里可以直接用加分的方法
            int score = userInfoService.selectGoldnumById(rankVO.getId());
            communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_DAY_GOLD, rankVO.getId(), score);

            //communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_DAY_GOLD,rankVO);
        }
        System.out.println("------------------------今日金牌end-----------------------");


        System.out.println("------------------------本周金牌begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            // 这里不用判断value中是否存在vo对象,有的话会添加失败
            int score = userInfoService.selectGoldnumById(rankVO.getId());
            communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_WEEK_GOLD, rankVO.getId(), score);

            //communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_WEEK_GOLD,rankVO);
        }
        System.out.println("------------------------本周金牌end-----------------------");



        System.out.println("------------------------本月金牌begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            int score = userInfoService.selectGoldnumById(rankVO.getId());
            communityRankVOService.addScore(RedisKeys.COMMUNITY_RANK_MONTH_GOLD, rankVO.getId(), score);

            //communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_MONTH_GOLD,rankVO);
        }
        System.out.println("------------------------本月金牌end-----------------------");






        // =================================================================================

        System.out.println("------------------------今日回复begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_DAY_ANSWER,rankVO);
        }
        System.out.println("------------------------今日回复end-----------------------");


        System.out.println("------------------------本周回复begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_WEEK_ANSWER,rankVO);
        }
        System.out.println("------------------------本周回复end-----------------------");



        System.out.println("------------------------本月回复begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_MONTH_ANSWER,rankVO);
        }
        System.out.println("------------------------本月回复end-----------------------");


        // ===========================================================================


        System.out.println("------------------------今日的顶begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_DAY_THUMB,rankVO);
        }
        System.out.println("------------------------今日的顶end-----------------------");


        System.out.println("------------------------本周的顶begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_WEEK_THUMB,rankVO);
        }
        System.out.println("------------------------本周的顶end-----------------------");


        System.out.println("------------------------本月的顶begin-----------------------");
        for (CommunityRankVO rankVO : rankVOList) {
            communityRankVOService.setZsetValue(RedisKeys.COMMUNITY_RANK_MONTH_THUMB,rankVO);
        }
        System.out.println("------------------------本月的顶end-----------------------");*/





        //初始化热门攻略
/*        System.out.println("---------------begin---------------");
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            int num = detail.getReplynum() + detail.getViewnum();
            strategyStatisVOService.addHot(detail.getId(), num);
        }
        System.out.println("---------------end---------------");*/

       //初始化所有攻略
       /* System.out.println("---------------begin---------------");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            //判断是否存在vo对象
            if (strategyStatisVOService.hasVoKey(detail.getId())) {
                continue;
            }
            vo = new StrategyStatisVO();
            BeanUtil.copyProperties(vo, detail);
            vo.setDestName(detail.getDest().getName());
            vo.setDestId(detail.getDest().getId());
            vo.setStrategyId(detail.getId());
            //存入vo对象
            strategyStatisVOService.setStrategyStatisVO(vo);
        }
        System.out.println("---------------end---------------");*/

    }
}
